<template>
	<!-- 通用版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('public.cutCode')" />
		<!-- 已选择工序 -->
		<zy-group :title="$t('homepage.ProductionMonitor.choosedProcedure')">
			<zy-table ref="zyTable" :headList="setList" :dataList="setData" checkbox
				@handleSelectionChange="handleSelectionChange" :loading="setLoading" border></zy-table>
		</zy-group>
		<!-- 扫描工序 -->
		<zy-group :title="$t('homepage.ProductionMonitor.scannedProcedure')">
			<scroll-view scroll-y="true" style="height:60vh">
				<zy-forms :model="formData" border labelWidth="160">
					<zy-form-item :label="$t('public.productionNo')+$t('public.colon')"><text
							class="formText">{{formData.productionNo || $t('public.null')}}</text>
					</zy-form-item>
					<zy-form-item :label="$t('homepage.ProductionMonitor.poductionNum')+$t('public.colon')"><text
							class="formText">{{formData.styleCode || $t('public.null')}}</text>
					</zy-form-item>
					<zy-form-item :label="$t('homepage.ProductionMonitor.poductionName')+$t('public.colon')"><text
							class="formText">{{formData.styleName || $t('public.null')}}</text>
					</zy-form-item>
					<zy-form-item :label="$t('public.cutNo')+$t('public.colon')"><text
							class="formText">{{formData.cuttingSheetNo || $t('public.null')}}</text>
					</zy-form-item>
					<zy-form-item :label="$t('homepage.ProductionMonitor.partName')+$t('public.colon')"><text
							class="formText">{{formData.partName || $t('public.null')}}</text>
					</zy-form-item>
					<zy-form-item :label="$t('homepage.ProductionMonitor.batchColor')+$t('public.colon')"><text
							class="formText">{{formData.color || $t('public.null')}}</text></zy-form-item>
					<zy-form-item :label="$t('homepage.ProductionMonitor.batchSize')+$t('public.colon')"><text
							class="formText">{{formData.size || $t('public.null')}}</text></zy-form-item>
					<zy-form-item :label="$t('homepage.ProductionMonitor.batchNo')+$t('public.colon')"><text
							class="formText">{{formData.bindingNo || $t('public.null')}}</text>
					</zy-form-item>
					<zy-form-item :label="$t('homepage.ProductionMonitor.batchNum')+$t('public.colon')"><text
							class="formText">{{formData.perBindNum || $t('public.null')}}</text>
					</zy-form-item>
					<zy-form-item :label="$t('public.remarkInfo')+$t('public.colon')"><text
							class="formText">{{formData.remarks || $t('public.null')}}</text></zy-form-item>
				</zy-forms>
			</scroll-view>
		</zy-group>
		<!-- 底部按钮 -->
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.print')" @rightBtn="printBtn"
			@leftBtn="resetBtn" :rightDisabled="disabled" :leftDisabled="disabled">
		</bottomBtn>
	</view>
</template>

<script>
	import {
		queryProcedureByCurrentUserDept,
		mobileTaskReport
	} from "@/api/homepage.js"
	
	import {
		queryListProcedure
	} from "@/api/index.js"
		
	const print = uni.requireNativePlugin('Sunmi-Print-Inner');
	var isConnect = false;
	export default {
		data() {
			return {
				//表格表头
				setList: [{
						name: this.$t('public.procedureCode'),
						key: 'procedureCode',
					},
					{
						name: this.$t('public.procedureName'),
						key: 'procedureName',
					}
				],
				setData: [], //表格数据
				setLoading: true,
				//表单字段
				formData: {
					productionNo: '',
					styleCode: '',
					styleName: '',
					cuttingSheetNo: '',
					partName: '',
					color: '',
					size: '',
					bindingNo: '',
					perBindNum: '',
					remarks: '',
				},
				procedureId: '',
				selectedId: '',
				disabled: true,
				barcode: "", //扫码数据
			}
		},
		onLoad() {
			//PDA扫描
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.searchList(data.code)
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		mounted() {
			this.getList(1)
		},
		methods: {
			//获取已选择工序表格数据
			getList(type) {
				if (type == 1) {
					queryListProcedure({
						foreignId: this.$store.state.loginData.id, //人员id
					}).then((res) => {
						if (res.code == 0) {
							this.setData = res.data
						} else {
							this.setData = []
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 3000
							})
						}
					}).catch((res) => {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					});
				} else {
					queryProcedureByCurrentUserDept().then((res) => {
						if (res.code == 0) {
							this.setData = res.data
						} else {
							this.setData = []
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 3000
							})
						}
					}).catch((res) => {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					});
				}
			},
			// 点击多选框
			handleSelectionChange(e) {
				this.selectedId = ''
				if (e.value.length > 0) {
					for (let i = 0; i < e.value.length; i++) {
						if (i < e.value.length - 1) {
							this.selectedId += e.value[i].procedureId + ','
						} else {
							this.selectedId += e.value[i].procedureId
						}
					}
				} else {
					this.selectedId = ""
				}
			},
			//搜索内容查询
			searchList(e) {
				this.procedureId = ''
				if (this.selectedId == '') {
					// for (let i = 0; i < this.setData.length; i++) {
					// 	if (i < this.setData.length - 1) {
					// 		this.procedureId += this.setData[i].procedureId + ','
					// 	} else {
					// 		this.procedureId += this.setData[i].procedureId
					// 	}
					// }
					
					uni.showToast({
						title: this.$t('homepage.procNull'),
						icon: "none",
						duration: 3000
					})
				} else {
					this.procedureId = this.selectedId
				}
				
				this.barcode = e
				mobileTaskReport({
					// barcode: e,
					barcode: this.barcode,
					procedureId: this.procedureId
				}).then((res) => {
					console.log(res)
					if (res.code == 0) {
						this.formData = res.data
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
						this.disabled = false
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			// 重置事件
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/ProductionMonitor/index"
				})
			},
			// 打印事件
			printBtn() {
				print.connect(res => {
					isConnect = res.connect == "hello"
				})
				if (isConnect) {
					//打印小票
					print.printQrCode({
						text: this.barcode,
						size: 6,
						errorlevel: 0,
						align: 1
					})
					//空白行分割之前打印内容（类似走空白）
					print.printDividingline({
						style: "4",
						height: "10"
					})
					print.printText({
						text: this.$t('public.orderNo') + this.$t('public.colon') + this.barcode,
						align: 1,
						size: 28,
						bold: true,
					})
					print.labelOutput()
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.formText {
		color: #999;
	}
</style>